package com.innerchic.weqiuqiu.app;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.innerchic.mapper.log.Logger;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/17 15:37
 * 包    名：com.innerchic.mapper.adapter
 * 描    述：RecyclerView项滑动监听
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class RecyclerItemTouchHelper extends ItemTouchHelper.Callback {

    private final RecyclerItemTouchHelper.ItemTouchHelperCallback callback;

    public RecyclerItemTouchHelper(RecyclerItemTouchHelper.ItemTouchHelperCallback callback) {
        this.callback = callback;
    }

    /**
     * 设置滑动类型标记
     *
     * @param recyclerView 列表
     * @param viewHolder   适配器
     * @return 返回一个整数类型的标识，用于判断Item那种移动行为是允许的
     */
    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        //START右向左 END左向右 LEFT向左 RIGHT向右  UP向上 DOWN向下
        //如果某个值传0，表示不触发该操作，次数设置支持上下拖拽，支持向右滑动
        return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.END | ItemTouchHelper.START);
    }

    /**
     * Item是否支持长按拖动
     *
     * @return true  支持长按操作
     * false 不支持长按操作
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    /**
     * Item是否支持滑动
     *
     * @return true  支持滑动操作
     * false 不支持滑动操作
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    /**
     * 拖拽切换Item的回调
     *
     * @param recyclerView 列表
     * @param viewHolder   拖动holder
     * @param target       目标holder
     * @return 如果Item切换了位置，返回true；反之，返回false
     */
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return callback.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    }

    /**
     * 滑动Item
     *
     * @param viewHolder holder
     * @param direction  滑动方向及距离
     *                   32右
     *                   16左
     *                   1上
     */
    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        Logger.d("Direction:" + direction);
        if (direction == 32) {
            callback.onItemRight(viewHolder.getAdapterPosition());
        } else if (direction == 16) {
            callback.onItemLeft(viewHolder.getAdapterPosition());
        } else if (direction == 1) {
            callback.onItemTop(viewHolder.getAdapterPosition());
        } else {
            callback.onItemBottom(viewHolder.getAdapterPosition());
        }
    }

    /**
     * Item被选中时候回调
     *
     * @param viewHolder  holder
     * @param actionState 当前Item的状态
     *                    ItemTouchHelper.ACTION_STATE_IDLE   闲置状态
     *                    ItemTouchHelper.ACTION_STATE_SWIPE  滑动中状态
     *                    ItemTouchHelper#ACTION_STATE_DRAG   拖拽中状态
     */
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * item触摸事件回调
     */
    public interface ItemTouchHelperCallback {
        void onItemRight(int position);

        void onItemLeft(int position);

        void onItemTop(int position);

        void onItemBottom(int position);

        boolean onMove(int fromPosition, int toPosition);
    }

}
